<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <img
          style="width: 13%;"
          src="../assets/logo3.jpg"
          alt="商城logo"
        >
        <span style="font-size: 25px;margin-left: 10px;">电商管理系统</span>
      </div>
      <div class="header-right">
        <el-button
          type="info"
          @click="logout"
        >退出</el-button>
      </div>
    </el-header>

    <!-- 左边侧边栏 -->
    <el-container>
      <el-aside :width="collapse ? '64px' : '200px'">
        <button class="toggle-button" @click="toggleCollapse">|||</button>
        <!-- 一级菜单 -->
        <!-- 
        unique-opened=true：是否只保持一个子菜单的展开
        -->
        <el-menu
          background-color="#373744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse = "collapse"
          :collapse-transition = false
          unique-opened
          router
        >
          <el-submenu
            :index="String('/' + item.path)"
            v-for="(item,index) in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="String('/' + chilItem.path)"
              v-for="(chilItem, chilIndex) in item.children"
              :key="chilItem.id"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{chilItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>

        <!-- <el-footer>Footer
        </el-footer> -->
      </el-container>
    </el-container>
  </el-container>
</template> 

<script>
import $ from "jquery";
export default {
  data() {
    return {
      menuList: [],
      icons: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 是否折叠
      collapse: false
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    // 退出登陆
    logout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    async getMenuList() {
      var { data: res } = await this.$http.get("/menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log("菜单栏", res);
    },
    // 点击按钮切换侧边栏的收缩和展开
    toggleCollapse(){
      this.collapse = !this.collapse;
    }
  },
};
</script>

<style scoped>
.toggle-button{
    background-color: #4a5074;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    width: 100%;
    border: none;
    cursor: pointer;
    outline: none;
}
.toggle-button:active{
  /*  */
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.header-left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 5px;
}
.el-aside {
  background-color: #373744;
}
.el-main {
  background-color: #eaedf1;
}
</style>